<?php
header("Content-type:text/html;charset=UTF-8");
// http://www.runoob.com/php/php-mysql-connect.html

//$servername = "127.0.0.1";
$servername = "mysql";
$username = "root";
$password = "123456";


// ############ 方法1 ##########

// 创建连接
$conn = new mysqli($servername, $username, $password);
// 检测连接
if ($conn->connect_error) {
	die("连接失败[实例 (MySQLi - 面向对象)]: " . $conn->connect_error);
}
echo "连接成功[实例 (MySQLi - 面向对象)]";
$conn->close();

echo "<hr/>";

// ############ 方法2 ##########
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
	die("Connection failed[实例 (MySQLi - 面向过程)]: " . mysqli_connect_error());
}
echo "连接成功 [实例 (MySQLi - 面向过程)]";
mysqli_close($conn);

echo "<hr/>";

// ############ 方法3 ##########
try {
	$conn = new PDO("mysql:host=$servername;dbname=information_schema", $username, $password);
	echo "连接成功[实例 (PDO)]";
}catch(PDOException $e) {
	echo $e->getMessage();
}
$conn = null;